/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


/**
 *
 * @author Administrator
 */
public class Connector {
    
    private static Connection conn = null;
    private static String username;
    private static String password;
    private static String hostname;
    private static String port;
    private static String database;

    public static Connection getConnect() {
       return conn;
     
    }

    public static String getUsername() {
        return username;
    }

    public static String getPassword() {
        return password;
    }

    public static String getHostname() {
        return hostname;
    }

    public static String getPort() {
        return port;
    }

    public static String getDatabase() {
        return database;
    }
    
    // Doc du lieu ket noi toi co so du lieu;
    
    public static void readConfig() {
        try {
            File fXmlFile = new File("config.xml");
            if (fXmlFile.exists()) {
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(fXmlFile);
                doc.getDocumentElement().normalize();

                NodeList nList = doc.getElementsByTagName("connection");

                for (int temp = 0; temp < nList.getLength(); temp++) {

                    Node nNode = nList.item(temp);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                        Element eElement = (Element) nNode;

                        hostname    = getTagValue("hostname", eElement);
                        port        = getTagValue("port", eElement);
                        username    = getTagValue("username", eElement);
                        password    = getTagValue("password", eElement);
                        database    = getTagValue("database", eElement);

                    }
                }
            }
        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
        }
    }
    // Lay gia tri theo tag name
    private static String getTagValue(String sTag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

        Node nValue = (Node) nlList.item(0);

        return nValue.getNodeValue();
    }
    //Tao ket noi
    public static boolean connect() {
        //hostname = "localhost";
        //port = "3306";
        //database = "booklib";
        //username = "root";
        //password = "123456";
        readConfig();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://" + hostname + ":" + port
                    + "/" + database+"?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true", username, password );
            return true;
        } catch (SQLException ex) {
            return false;
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }

        return false;
    }
    
    // Dong ket noi 
    public static void closeConnect() {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }    
        
}
